package _60_二叉树层序遍历;

import java.util.ArrayList;
import java.util.LinkedList;

class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}

public class Solution {
    ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        if(pRoot==null) return res;

        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.offerLast(pRoot);
        int size = 0;
        while(!queue.isEmpty()){
            size = queue.size();
            ArrayList<Integer> layer = new ArrayList<>();
            while(size>0){
                size--;
                TreeNode head  = queue.pollFirst();
                layer.add(head.val);
                if(head.left!=null) queue.offerLast(head.left);
                if(head.right!=null) queue.offerLast(head.right);
            }
            res.add(layer);
        }
        return res;
    }

}